package cn.wizzer.app.sys.modules.services.impl;

import cn.wizzer.app.sys.modules.models.SysErcAddress;
import cn.wizzer.app.sys.modules.services.SysErcAddressService;
import cn.wizzer.framework.base.service.BaseServiceImpl;
import com.alibaba.dubbo.config.annotation.Service;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.plugins.wkcache.annotation.CacheDefaults;
import org.nutz.plugins.wkcache.annotation.CacheRemoveAll;
import org.nutz.plugins.wkcache.annotation.CacheResult;

import java.util.List;
import java.util.Random;

@IocBean(args = {"refer:dao"})
@Service(interfaceClass = SysErcAddressService.class)
@CacheDefaults(cacheName = "sys_erc_address")
public class SysErcAddressServiceImpl extends BaseServiceImpl<SysErcAddress> implements SysErcAddressService {
    public SysErcAddressServiceImpl(Dao dao) {
        super(dao);
    }

    @CacheResult
    @Override
    public List<SysErcAddress> allUseful() {
        return this.query(
                Cnd.where("disabled", "=", 0)
                        .and("delFlag","=",0)
        );
    }

    @Override
    public SysErcAddress randomAddress() {
        List<SysErcAddress> sysErcAddresses = this.allUseful();
        return sysErcAddresses.size()==0?null:sysErcAddresses.get(new Random().nextInt(sysErcAddresses.size()));
    }

    @Override
    public void add(SysErcAddress address){
        this.insert(address);
        this.clearCache();
    }

    @Override
    public void vDel(String id){
        this.vDelete(id);
        this.clearCache();
    }

    @Override
    @CacheRemoveAll
    public void clearCache(){

    }



}